package com.fantastic.web.controller.app;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.result.ResultData;
import com.fantastic.common.utils.CheckUtil;
import com.fantastic.common.utils.JwtUtils;
import com.fantastic.common.utils.RandomUtil;
import com.fantastic.common.utils.RespUtil;
import com.fantastic.model.pojo.zjs.Account;
import com.fantastic.model.pojo.zjs.User;
import com.fantastic.model.vo.zjs.UserVo;
import com.fantastic.service.zjs.AccountService;
import com.fantastic.service.zjs.AuthService;
import com.fantastic.service.zjs.RegisService;
import com.github.benmanes.caffeine.cache.Cache;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: rxx
 * @since: 2024/5/28 09:38
 * @description: 客户端账号信息表请求控制器
 */
@RestController
@RequestMapping("/app")
public class AccountController {

    @Resource
    private Cache<Object, Object> cache;

    @Resource
    private AccountService accountService;

    @Resource(name = "appAuth")
    private AuthService authService;

    @Resource
    private RegisService regisService;

    /**
     * 客户端登录
     * 使用手机号验证登录
     *
     * @param account
     * @return
     */
    @PostMapping("/login")
    public ResultData loginAccount(Account account,String code, HttpServletResponse response) {
        //验证码验证
        CheckUtil.checkString(RespStatusInfo.CODE_ERROR,
                (String) cache.getIfPresent(RandomUtil.codeKey),
                code);
        //进行登录验证，手机号存在返回用户信息
        UserVo user = authService.login(account);
        //创建Token并设置给响应头
        RespUtil.addToken("uid", user.getUser().getUserId(), 31536000L, response);
        return new ResultData(RespStatusInfo.OK_LOGIN, user);
    }

    /**
     * 注册
     * 两种情况：
     * 1、员工账号由后台分配。是先有员工信息，再由员工信息id注册账号。
     * 2、普通用户账号自行中注册，注册后便要添加默认的用户信息。
     * 账号密码注册 | | 手机号验证码注册
     *
     * @param account 账号信息
     * @return
     */
    @PostMapping("/register")
    public ResultData registerAccount(@RequestBody Account account) {
        return new ResultData(RespStatusInfo.OK_REGIS, regisService.regis(account));
    }

    /**
     * 查询单个账号信息
     *
     * @param accountId
     * @return
     */
    @PostMapping("/getAccountApp")
    public ResultData getAccountApp(Integer accountId) {
        return new ResultData(RespStatusInfo.OK, accountService.getAccount(accountId));
    }

    /**
     * 修改账号信息
     *
     * @param account
     * @return
     */
    @PostMapping("/updateAccountApp")
    public ResultData updateAccountApp(Account account) {
        return new ResultData(RespStatusInfo.OK, accountService.updateAccount(account));
    }

    /**
     * 删除账号信息
     *
     * @param accountId
     * @return
     */
    @PostMapping("/deleteAccountApp")
    public ResultData deleteAccountApp(Integer accountId) {
        return new ResultData(RespStatusInfo.OK, accountService.deleteAccount(accountId));
    }

}
